package com.bhs.sansonglogistics.ui.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.ArriveBean;
import com.bhs.sansonglogistics.bean.Department;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 增加部门
 */
public class AddDepartmentActivity extends BaseActivity implements TextWatcher, View.OnClickListener, NetCallBack {


    private EditText mEtDepartment;
    private EditText mEtManager;
    private EditText mEtPhone;
    private Button mBtnAdd;
    private Department department;
    private TextView mTvDelete;

    @Override
    public int initLayout() {
        return R.layout.activity_add_department;
    }

    @Override
    public void initView() {
        department = getIntent().getParcelableExtra("department");
        TextView mTvTitle = findViewById(R.id.tv_title);
        mBtnAdd = findViewById(R.id.btn_add);
        mTvDelete = findViewById(R.id.tv_delete);
        if (department != null) {
            mTvTitle.setText("修改部门");
            mBtnAdd.setText("确认修改");
            mTvDelete.setVisibility(View.VISIBLE);
        } else {
            mTvTitle.setText("增加部门");
        }
        mEtDepartment = findViewById(R.id.et_department);
        mEtManager = findViewById(R.id.et_manager);
        mEtPhone = findViewById(R.id.et_phone);
        mEtDepartment.addTextChangedListener(this);
        mEtManager.addTextChangedListener(this);
        mEtPhone.addTextChangedListener(this);
        mBtnAdd.setOnClickListener(this);


        mTvDelete.setOnClickListener(this);
    }

    @Override
    public void initData() {
        if(department!=null) {
            mEtDepartment.setText(department.getDepartment_name());
            mEtManager.setText(department.getDepartment_contact());
            mEtPhone.setText(department.getDepartment_mobile());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        verify();
    }

    private void verify() {
        boolean verify = !TextUtils.isEmpty(mEtDepartment.getText());
        if (TextUtils.isEmpty(mEtManager.getText())) {
            verify = false;
        }

        if (TextUtils.isEmpty(mEtPhone.getText())) {
            verify = false;
        }
        mBtnAdd.setEnabled(verify);
        mBtnAdd.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_add) {
            if (!MyUtils.isPhone(mEtPhone.getText().toString())) {
                ToastUtil.show("请输入正确的手机号码");
                return;
            }
            Map<String, String> map = new ArrayMap<>();
            map.put("department_name", mEtDepartment.getText().toString());
            map.put("department_contact", mEtManager.getText().toString());
            map.put("department_mobile", mEtPhone.getText().toString());
            if (department != null) {
                map.put("department_id", String.valueOf(department.getDepartment_id()));
                networkRequest(netApi.edit_department(map), this, 666);
            } else {
                networkRequest(netApi.add_department(map), this, 666);
            }
        }else if(view.getId() == R.id.tv_delete){
            new XPopup.Builder(mActivity)
                    .asConfirm("提示", "确定删除吗",
                            "取消", "确定",
                            new OnConfirmListener() {
                                @SuppressLint("NotifyDataSetChanged")
                                @Override
                                public void onConfirm() {
                                    networkRequest(netApi.del_department(department.getDepartment_id()), AddDepartmentActivity.this, 666);
                                }
                            }, null, false)
                    .show();
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        ToastUtil.show(bean.getMsg());
        if (bean.isStatus()) {
            finish();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}